#include <i264e_common.h>
#ifdef DMALLOC
#include <imp/dmalloc.h>
#endif
int i264e_update_fenc(i264e_t *h, i264e_frame_t *fenc, i264e_pic_t *src)
{
	if ((src->img.i_csp < C_CSP_I420) || (src->img.i_csp > C_CSP_T420)) {
		i264e_log(h, C_LOG_ERROR, "Invalid input colorspace:%d\n", src->img.i_csp);
		goto err_input_csp;
	}

	if ((src->img.i_stride[0] < C_ALIGN(h->common.param.i_width, 16)) || (src->img.i_stride[1] < C_ALIGN(h->common.param.i_width, 16)/2)) {
		i264e_log(h, C_LOG_ERROR, "invalidate input picture stride:(%d, %d)\n",
				src->img.i_stride[0], src->img.i_stride[1]);
		goto err_input_stride;
	}

	if (src->img.i_csp != C_CSP_T420 && ((src->img.i_lines[0] < C_ALIGN(h->common.param.i_height, 16)) || (src->img.i_lines[1] < C_ALIGN(h->common.param.i_height, 16) / 2))) {
		i264e_log(h, C_LOG_ERROR, "invalidate input picture lines(%d, %d)\n",
				src->img.i_lines[0], src->img.i_lines[1]);
		goto err_input_lines;
	}
	if (h->common.param.b_user_nalbuffer) {
		if ((unsigned int)(*(src->nals_buffer))%4 != 0) {
			i264e_log(h, C_LOG_ERROR, "invalidate input nal buffer align\n");
			goto err_nal_buf_align;
		}

		// This may have risk to define RAW_STREAM_OFFSET to 8192, and has logical conflict when start nal not equal to 0;
		// But in SDK and current i264ecli usage environment, this logical confilct has no effect, so don't worry
		h->cur->p.out.p_bitstream = *(src->nals_buffer) + RAW_STREAM_OFFSET;
		h->cur->p.out.i_bitstream = src->nals_buffer_size - RAW_STREAM_OFFSET;
	}
	fenc->i_csp			= h->common.param.i_csp;
	fenc->i_qpplus1		= src->i_qpplus1;
	fenc->i_pts			= src->i_pts;
	fenc->timestamp		= src->timestamp;
	fenc->i_pic_struct	= src->i_pic_struct;
	fenc->extra_sei		= src->extra_sei;
	fenc->opaque		= src->opaque;
	fenc->b_force_idr	= src->b_force_idr;

	if((fenc->i_pic_struct < C_PIC_STRUCT_AUTO) || (fenc->i_pic_struct > C_PIC_STRUCT_TRIPLE)) {
		fenc->i_pic_struct = C_PIC_STRUCT_AUTO;
	}

	if (fenc->i_pic_struct == C_PIC_STRUCT_AUTO) {
		fenc->i_pic_struct = C_PIC_STRUCT_PROGRESSIVE;
	}
	memcpy(&h->cur->p.pic_in, src, sizeof(i264e_pic_t));
	memcpy(&h->cur->p.pic_out, src, sizeof(i264e_pic_t));

	if (h->common.param.b_user_fenc) {
		fenc->plane[0] = src->img.plane[0];
		fenc->plane[1] = src->img.plane[1];
		fenc->plane_pa[0] = src->img.plane_pa[0];
		fenc->plane_pa[1] = src->img.plane_pa[1];
		fenc->i_stride[0] = src->img.i_stride[0];
		fenc->i_stride[1] = src->img.i_stride[1];
		fenc->i_lines[0] = src->img.i_lines[0];
		fenc->i_lines[1] = src->img.i_lines[1];
	} else {
		h->common.mc.copy_frame[c_mc_type(fenc->i_csp, src->img.i_csp)](fenc->plane,
				fenc->i_stride, src->img.plane, (int *)src->img.i_stride,
				h->common.param.i_width, h->common.param.i_height);
#if 0
		if (h->common.param.i_height%16 != 0) {
			uint32_t i_height;
			for (i_height=h->common.param.i_height; i_height < src->img.i_lines[0]; i_height++) {
				memcpy((fenc->plane[0]+h->common.param.i_width*i_height), (fenc->plane[0]+h->common.param.i_width*(i_height-1)), h->common.param.i_width);
			}
			for (i_height=h->common.param.i_height/2; i_height < src->img.i_lines[1]; i_height++) {
				memcpy((fenc->plane[1]+h->common.param.i_width*i_height), (fenc->plane[1]+h->common.param.i_width*(i_height-1)), h->common.param.i_width);
			}
		}
#endif
	}

	if ((h->common.param.soc == C_T30) || (h->common.param.soc == C_T21) || (h->common.param.soc == C_T23)) {
		fenc->b_use_ncu = src->img.is_use_ncu;
		fenc->ncu_frame_info = src->img.ncu_frame_info;
		if ((h->common.param.soc == C_T21) || (h->common.param.soc == C_T23)) {
			fenc->b_use_aezone = src->img.is_use_aezone;
			fenc->ae_zone_info = src->img.ae_zone_info;
		}
	}

	return 0;
err_nal_buf_align:
err_input_lines:
err_input_stride:
err_input_csp:
	return -1;
}
